// 17.电话号码的字母组合
package Leetcode_Second_Brush;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution017 {
    List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0)
            return result;

        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        backtrace(digits, 0, map, new StringBuilder());
        return result;
    }

    private void backtrace(String digits, int cur, Map<Character, String> map, StringBuilder sb) {
        if (cur == digits.length()) {
            result.add(sb.toString());
            return;
        }

        for (char c : map.get(digits.charAt(cur)).toCharArray()) {
            sb.append(c);
            backtrace(digits, cur + 1, map, sb);
            sb.deleteCharAt(cur);
        }

    }
}
